257A - Sockets - CodeForces Solution


greedy implementation sortings *1100

Please click on ads to support us..

Python Code:

n, m, k = map(int, input().split())
filters = [int(i) for i in input().split()]
filters.sort()
ports = k
count = 0
while ports < m and filters:
    ports += filters[-1] - 1
    filters.pop()
    count += 1
if ports >= m:
    print(count)
else:
    print(-1)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
 
#define endl '\n'
#define int long long
 
const int MOD = 1e9 + 7;
const int INF = LLONG_MAX >> 1;

signed main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,m,k;
    cin >> n >> m >> k;
    int a[n];
    for(int i = 0; i < n; ++i)cin >> a[i];

	sort(a,a+n, greater<int>());
	if(k >= m)cout << "0";
	else{
		int sum = a[0] + k - 1;
		if(sum >= m){
	    		cout << "1";
	    		return 0;
	        }
	    else{
		    for(int i = 1; i < n; ++i){
		        sum += a[i] - 1;
		    	// cout << sum << i << endl;
		      	if(sum >= m){
		    		cout << i+1;
		    		return 0;
		        }
		    }
		}
    	cout << "-1";
	}
	
}


Comments

Submit
0 Comments
More Questions

714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom
219A - k-String
952A - Quirky Quantifiers
451B - Sort the Array
1505H - L BREAK into program
171E - MYSTERIOUS LANGUAGE
630D - Hexagons
1690D - Black and White Stripe
1688D - The Enchanted Forest
1674C - Infinite Replacement
712A - Memory and Crow
1676C - Most Similar Words